#ifndef __UART_FUN_H
#define __UART_FUN_H
/***********************
#define N81 0         // no   parity 8 bit  
#define E71 1         // even parity 7 bit  
#define E81 2         // even parity 8 bit  
#define O71 3         // ODD  parity 7 bit  PORT2  
#define N71 4         // no   parity 7 bit  PORT2  
#define O81 5         // ODD  parity 8 bit  
#define N82 10        // no   parity 8 bit stop 2 bit 
#define E82 11        // even parity 8 bit stop 2 bit 
#define O82 12        // ODD  parity 8 bit stop 2 bit 
*************************/
#define URX_LEN0  1100
#define URX_LEN1  1100
#define URX_LEN2   300
#define URX_LEN3   300

#define UTX_LEN0  1100
#define UTX_LEN1  1100
#define UTX_LEN2   300 
#define UTX_LEN3   300 

#define URX_USE_LEN (URX_LEN0+URX_LEN1+URX_LEN2+URX_LEN3+10)
#define UTX_USE_LEN (UTX_LEN0+UTX_LEN1+UTX_LEN2+UTX_LEN3+10)

//////////////////////////////////////////////

#ifdef FIRST
#define EXTERN 
short dpath=-1,DBG_PATH=-1;

#if ((URX_USE_LEN+UTX_USE_LEN)>UTR_BUF_SIZE)
UTR_error size over UTR_BUF_SIZE;
#endif

EXTERN U8 *URX_BUFF=(U8 *)UTR_BUF_AP;
EXTERN U8 *UTX_BUFF=(U8 *)(UTR_BUF_AP+URX_USE_LEN);

#else
#define EXTERN  extern
extern short dpath,DBG_PATH;

EXTERN U8 *URX_BUFF;
EXTERN U8 *UTX_BUFF;
#endif
EXTERN char dmsg[128];

typedef struct { 
   U16  rx_head,rx_tail,rx_max_buf;
   U8   *rx_buf; 
   U16  tx_head,tx_tail,tx_max_buf;
   U8   *tx_buf; 
   U8   tx_int;
   U8   rxdly; 
} UART_PARM;

#define UART_NUM  4
EXTERN UART_PARM  UART[UART_NUM];   

#if (UART_NUM>=1)
#define UART0
void           init_port0(int baud,char mode);
#endif

#if (UART_NUM>=2)
#define UART1
void           init_port1(int baud,char mode);
#endif

#if (UART_NUM>=3)
#define UART2
void           init_port2(int baud,char mode);
#endif

#if (UART_NUM>=4)
#define UART3
void           init_port3(int baud,char mode);
#endif


///////////////////////////////////
void           uart_init_port(short ppath,int baud,char mode);
void           uart_rx_clr_port(short ppath);
unsigned char  uart_rx_buf_read(short ppath);
unsigned char  *uart_rx_buf(short ppath);
unsigned short uart_rx_rdy(short ppath);
unsigned short uart_rx_max(short ppath);
void           uart_rx_buf_write(short ppath,char data);
unsigned char  uart_rx_buf_char(short);

void           uart_tx_clr_port(short ppath);
void           uart_tx_putch(short ppath,char data);
unsigned short uart_tx_rdy(short ppath);
unsigned char  uart_tx_buf_read(short ppath);
void           uart_tx_buf_write(short ppath,unsigned char data);
void           uart_tx_int_dis(short ppath);
void           uart_tx_int_ena(short ppath);
unsigned char  uart_tx_int(short ppath);
unsigned char  *uart_tx_buf(short ppath);
void           uart_tx(short ppath,unsigned short dlen);
unsigned short uart_tx_max(short ppath);

void           uart_rts_ena(short ppath);
void           uart_rts_dis(short ppath);
unsigned char  uart_protocol(short ppath);
unsigned char  uart_pro(void);

void           dputstr(short ppath,char *data);
unsigned short Pwrite(short ppath,unsigned char *data,unsigned short dlen);
unsigned short Pread(short ppath,unsigned char *data,unsigned short dlen);
void Dprintf(const char *fmt, ...);
void Pprintf(int ppath,const char *fmt, ...);
void Pputch(int ppath,char ch);
///////////////////////////////////////

#endif
